package cn.e4j.modular.system.service.impl;


import cn.e4j.core.dto.impl.Dto;
import cn.e4j.modular.system.entity.SysRole;
import cn.e4j.modular.system.entity.SysRoleCmp;
import cn.e4j.modular.system.entity.SysRoleModule;
import cn.e4j.modular.system.entity.SysUserRole;
import cn.e4j.modular.system.mapper.SysRoleMapper;
import cn.e4j.modular.system.service.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色资料表 服务实现类
 * </p>
 *
 * @author yuan_rz
 * @since 2019-01-04
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getRolesList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> roleDtos = null;
        if(page!=null){
            roleDtos = this.baseMapper.listRolesPage(page, dto);
        }else{
            roleDtos = this.baseMapper.listRoles(dto);
        }
        return roleDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getRoles() {
        List<Map<String, Object>> roleDtos = this.baseMapper.listRoles();
        List<Map<String, Object>> roleList = new ArrayList<>();
        for(Map<String,Object> map:roleDtos){
            Map<String,Object> roleMap=new HashMap<>();
            roleMap.put("code",map.get("CODE"));
            roleMap.put("value",map.get("VALUE"));
            roleList.add(roleMap);
        }
        return roleList;
    }

    @Transactional(readOnly = true)
    public List<SysRole> listRolesExport(Dto inDto) {
        List<SysRole> sysRoleList = this.baseMapper.listRolesExport(inDto);
        return sysRoleList;
    }

    /**
     * 增加
     * @param sysRole
     * @return
     */
    @Transactional
    public void add(SysRole sysRole){
        sysRole.setIS_ENABLE("1");
        sysRole.insert();
    }

    /**
     * 修改
     * @param sysRole
     */
    @Transactional
    public void edit(SysRole sysRole){
        sysRole.updateById();
    }

    /**
     * 删除
     * @param sysRole
     */
    @Transactional
    public void delete(SysRole sysRole){
        /*sysRole.setIS_ENABLE("0");
        sysRole.updateById();*/
        //删除角色及该角色下的用户
        sysRole.deleteById();
        SysUserRole sysUserRole = new SysUserRole();
        UpdateWrapper<SysUserRole> sysUserRoleUpdateWrapper = new UpdateWrapper<>();
        sysUserRoleUpdateWrapper.eq("ROLE_ID",sysRole.getID());
        sysUserRole.delete(sysUserRoleUpdateWrapper);

        //删除该角色的模块权限及组件权限
        SysRoleModule sysRoleModule = new SysRoleModule();
        UpdateWrapper<SysRoleModule> sysRoleModuleUpdateWrapper = new UpdateWrapper<>();
        sysRoleModuleUpdateWrapper.eq("ROLE_ID", sysRole.getID());
        sysRoleModule.delete(sysRoleModuleUpdateWrapper);

        SysRoleCmp sysRoleCmp = new SysRoleCmp();
        UpdateWrapper<SysRoleCmp> sysRoleCmpUpdateWrapper = new UpdateWrapper<>();
        sysRoleCmpUpdateWrapper.eq("ROLE_ID", sysRole.getID());
        sysRoleCmp.delete(sysRoleCmpUpdateWrapper);
    }

}
